package vn.intelsoft.vlogvn.adapter;

import java.util.List;

import vn.intelsoft.vlogvn.R;
import vn.intelsoft.vlogvn.imageloader.ImageLoader;
import vn.intelsoft.vlogvn.imageloader.ImageLoader1;
import vn.intelsoft.vlogvn.instance.Instance;
import vn.intelsoft.vlogvn.instance.Vlog;


import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class VlogAdapter extends BaseAdapter {
	private List<Vlog> vlogs = null;
	private LayoutInflater mLayoutInflater;
	private Activity mActivity;
	private ImageLoader1 mImageLoader;

	public VlogAdapter(Activity activity, List<Vlog> vlogs) {
		// TODO Auto-generated constructor stub
		mLayoutInflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mActivity = activity;
		mImageLoader = new ImageLoader1(mActivity);
		this.vlogs = vlogs;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (getVlogs() == null) {
			return 0;
		} else {
			return vlogs.size();
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return vlogs.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.vlog_item, null);
			viewHolder = new ViewHolder();
			
			viewHolder.imageview = (ImageView) convertView
					.findViewById(R.id.imageview);
			LayoutParams layoutParams = (LayoutParams) viewHolder.imageview.getLayoutParams();
			layoutParams.width = Instance.width(mActivity) / 3;
			layoutParams.height = Instance.width(mActivity) / 4;
			
			viewHolder.imageview.setLayoutParams(layoutParams);
			
			viewHolder.txtTitle = (TextView) convertView
					.findViewById(R.id.textview_title);
			convertView.setTag(viewHolder);
			
			viewHolder.likeCount = (TextView) convertView.findViewById(R.id.like);
			viewHolder.viewCount = (TextView) convertView.findViewById(R.id.view);
			viewHolder.duration = (TextView) convertView.findViewById(R.id.time);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Vlog vlog = getVlogs().get(position);
		if (vlog != null) {
			viewHolder.txtTitle.setText(vlog.title);
			mImageLoader.DisplayImage(vlog.thumbnailHd, viewHolder.imageview);
			
			viewHolder.likeCount.setText(vlog.likeCount + " lượt xem");
			viewHolder.viewCount.setText(vlog.viewCount + " thích" );
			viewHolder.duration.setText(Instance.getDurationString(vlog.duration));
		}

		return convertView;
	}

	public List<Vlog> getVlogs() {
		return vlogs;
	}

	public void setVlogs(List<Vlog> vlogs) {
		this.vlogs = vlogs;
	}

	private class ViewHolder {
		ImageView imageview;
		TextView txtTitle;
		TextView likeCount;
		TextView viewCount;
		TextView duration;
	}

}
